﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace C_sports_meet
{
    /// <summary>
    /// Login.xaml 的交互逻辑
    /// </summary>
    public partial class Login : Window
    {
        public Login()
        {
            InitializeComponent();
        }
        private void txtUserName_SelectionChanged(object sender, TextChangedEventArgs e)
        {
            TextBox textBox = (TextBox)sender;
            string selectedUserName = textBox.Text;           
            Debug.WriteLine("Selected UserName: " + selectedUserName); // 输出选择的用户名
            if (!string.IsNullOrEmpty(selectedUserName))
            {
                SqlConnection sqlConnection = new SqlConnection("server=;database=sportmeet;user=sa;pwd=Lss200217");
                sqlConnection.Open();
                string sql = "select Image from dbo.Users where UserName = @UserName";

                using (SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection))
                {
                    sqlCommand.Parameters.AddWithValue("@UserName", selectedUserName);
                    SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();

                    if (sqlDataReader.Read())
                    {
                        byte[] images = (byte[])sqlDataReader["image"];
                        MemoryStream stream = new MemoryStream(images);
                        BitmapImage bmp = new BitmapImage();
                        bmp.BeginInit();
                        bmp.StreamSource = stream;
                        bmp.EndInit();
                        M_Image.Source = bmp;
                    }
                    else
                    {
                        // 如果数据库中没有对应的账号，则清空图片显示
                        M_Image.Source = null;
                    }
                }
            }
            else
            {
                // 如果没有选择任何用户名，则清空图片显示
                M_Image.Source = null;
            }
        }
        public static string LoggedInPassword { get; set; }
        public static string LoggedOutUser { get; set; }
        private void login_Click(object sender, RoutedEventArgs e)//登录按钮点击事件
        {
            //连接数据库
            SqlConnection sqlConnection = new SqlConnection("server=;database=sportmeet;user=sa;pwd=Lss200217");
            sqlConnection.Open();
            string add_data = "select * from dbo.Users where UserName=@UserName and PassWord=@Password";
            SqlCommand cmd = new SqlCommand(add_data, sqlConnection);
            cmd.Parameters.AddWithValue("@UserName", txtUserName.Text);
            cmd.Parameters.AddWithValue("@PassWord", txtPwd.Password);
            cmd.ExecuteNonQuery();//返回命令影响的行数

            int count = Convert.ToInt32(cmd.ExecuteScalar());
            string user = txtUserName.Text;
            string password = txtPwd.Password;
            LoggedInPassword = password;
            LoggedOutUser = user;   


            if (count > 0)//数据库匹配成功
            {             
                MainWindow main = new MainWindow();
                this.Close();
                main.Show();
            }
            else
            {
                MessageBox.Show("账号或密码输入错误");
            }

            sqlConnection.Close();


        }


        private void register_Click(object sender, RoutedEventArgs e)//注册按钮点击事件
        {
            registPage reg1 = new registPage();
            this.Close();
            reg1.Show();
        }

        private void ckbPwd_Click(object sender, RoutedEventArgs e)//忘记密码按钮点击事件
        {
            ResetPwd resetPwd = new ResetPwd();
            this.Close();
            resetPwd.Show();
        }


    }
}
